package com.eephone.ep.views.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.eephone.ep.views.tool.SpecTools;

/**
 * 圆形图片的ImageView
 * Created by xyf on 2016/3/1.
 */
public class CircleImageView extends ImageView {

    /**
     * 原图片
     */
    private Bitmap mSrcBitmap;
    /**
     * View的高度
     */
    private int mHeight;
    /**
     * View的宽度
     */
    private int mWidth;

    public CircleImageView(Context context) {
        super(context);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initBitmap(attrs);
    }

    public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initBitmap(attrs);
    }

    /**
     * 从AttributeSet中获取到Src的图片资源
     *
     * @param attrs
     */
    private void initBitmap(AttributeSet attrs) {
        int bitmapId = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "src", 0);
        mSrcBitmap = BitmapFactory.decodeResource(getResources(), bitmapId);
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        mSrcBitmap = bm;
        super.setImageBitmap(bm);
        postInvalidate();
    }

    @Override
    public void setImageResource(int resId) {
        mSrcBitmap = BitmapFactory.decodeResource(getResources(), resId);
        super.setImageResource(resId);
        postInvalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /**
         * 根据内容设定View的宽高
         */
        mWidth = SpecTools.getSpecSizeByMode(widthMeasureSpec, mSrcBitmap, this);
        mHeight = SpecTools.getSpecSizeByMode(heightMeasureSpec, mSrcBitmap, this);
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        /**
         * 根据宽高的最小值来压缩图片
         */
        int min = Math.min(mWidth, mHeight);
        mSrcBitmap = Bitmap.createScaledBitmap(mSrcBitmap, min, min, false);
        canvas.drawBitmap(drawCircleImage(mSrcBitmap, min), 0, 0, null);
    }

    private Bitmap drawCircleImage(Bitmap source, int size) {
        /**
         * 设置画笔
         */
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        /**
         * 生成一个空的Bitmap
         * 并根据这个Bitmap生成一个Size大小的画布
         */
        Bitmap target = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(target);
        /**
         * 先绘制圆形
         */
        canvas.drawCircle(size / 2, size / 2, size / 2, paint);
        /**
         * 使用SRC_IN模式
         * 取source和target的交集，并且保留source
         * （所以叫src_in，不管你信不信，反正我是这么理解的）
         */
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        /**
         * 把source绘制到Target上面，
         * 交集形状，并且保留source的图形
         * 然后把完成的target返回
         */
        canvas.drawBitmap(source, 0, 0, paint);
        return target;
    }
}
